package com.gsm.service.impl;

import com.gsm.dao.WebSubjectDao;
import com.gsm.entity.Result;
import com.gsm.po.Subject;
import com.gsm.service.WebSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WebSubjectServiceImpl implements WebSubjectService {
    @Autowired
    private WebSubjectDao webSubjectDao;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Result selectSubjectList() {
        List<Subject> list = null;
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        list = (List<Subject>) ops.get("web:subject:list");
        if (list != null) {
            return Result.success(list);
        }
        list = webSubjectDao.selectSubjectList();
        ops.set("web:subject:list", list);
        return Result.success(list);
    }

    @Override
    public Result selectSubjectZero() {
        List<Subject> list = null;
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        list = (List<Subject>) ops.get("web:subject:list:zero");
        if (list != null) {
            return Result.success(list);
        }
        list = webSubjectDao.selectSubjectZero();
        ops.set("web:subject:list:zero", list);
        return Result.success(list);
    }

    @Override
    public Result selectSubjectInfoById(Long subjectId) {
        Subject subject = null;
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        subject = (Subject) ops.get("web:subject:id:" + subjectId);
        if (subject != null) {
            return Result.success(subject);
        }
        subject = webSubjectDao.selectSubjectInfoById(subjectId);
        ops.set("web:subject:id:" + subjectId, subject);
        return Result.success(subject);
    }

}
